The fifteenth day of Advent of Code was yesterday 15/12/2024! If you’re here for the first time today, don’t forget to read the posts about Day 1, Day 2, Day 3, Day 4, Day 5, Day 6, Day 7, Day 8, Day 9, Day 10, Day 11, Day 12, Day 13 and Day 14.
I classified the fifteenth problem as medium (part 1) and medium (part 2).
Tips
- The first part of the problem is to read the maze (warehouse) and the robot commands.
- Use the example inputs to test the solution.
- You need to detect in the maze input where the walls, robot, and boxes are.
- The commands are on multiple lines, it’s easier if you concatenate them into a single string.
- You can implement robot directions (commands) using vectors.
- Vectors can also be used to mark box and wall positions.
- The walls surround the entire warehouse, you don’t need to worry about the robot leaving the warehouse.
- To check if the robot can move, verify if the new position is free. In this case, move the robot by updating its position.
- If the next position is occupied by a wall, the robot cannot move and stays in the same place.
- To know if boxes can be moved, you can recursively try all boxes from the movement direction. For each box, check if it can move (blank space in the movement direction). If one of the boxes cannot be moved, the movement is canceled.
- Remember that the robot can push multiple boxes at once. A recursive function can help solve this problem.
- In part 1, boxes are the same size as walls, meaning each box occupies one position. In part 2, each box occupies two positions.
- Part 2 is a bit more complex because you can move more than columns and rows of boxes, but entire parts of the warehouse.
- Horizontal movement is similar to part 1, as each box has a height of only one character, like in part 1.
- Each movement in part 2 can move up to 2 boxes at once. But this movement is done in cascade, so these two boxes can move other 2, 3, or more boxes. Each box movement can move zero, one, or two boxes at each step, but can cause the movement of many others.
- In part 2, you need to verify if the entire movement is valid. For example, when the boxes being pushed form a V or get stuck on a wall, they all have to stop moving. If you implement the movement function as in part 1, you’ll see that the boxes will move in several pieces, which leads to the wrong answer.
How much Python do you need to know?
The chapters refer to my book Introduction to Programming with Python.